/*
 * Store.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;
/**
 * implements the Stores that will be used in the trail
 * @author Steven
 * @version 1.0 
 * As of now stores have unlimited inventory but that might change in the near future
 * 
 * The way this works is you do all the displaying and stuff with the temporary values
 * then when next is clicked you run it all 4 REALZ and then commit the inventory back to 
 * controller
 */
public class Store {
	/**
	 * playerInv - the player's inventory.
	 */
	private final Inventory playerInv;

	/**
	 * tempHullParts - the temporary number of hull parts.
	 */
	/**
	 * tempFood - the temporary number of food.
	 */
	/**
	 * tempFuelCells - the temporary number of fuel cells.
	 */
	/**
	 * tempMoney - the temporary number of moneys.
	 */
	private int tempMoney, tempFuelCells, tempFood, tempHullParts;

	/**
	 * tempTimesuits - the temporary number of time suits.
	 */
	/**
	 * tempTimeBulletBills - the temporary number of time bullet bills.
	 */
	/**
	 * tempEngines - the temporary number of engines.
	 */
	/**
	 * tempFCapacitors - the temporary number of fuel capacitors.
	 */
	private int tempFCapacitors, tempEngines, tempTimeBulletBills, tempTimesuits;

	/**
	 * engines - the instance of Engine.
	 */
	/**
	 * fCapacitors - the Instance of Flux Capacitors.
	 */
	/**
	 * hullParts - the Instance of hull parts.
	 */
	/**
	 * food - the instance of food.
	 */
	/**
	 * fuelCells - the instance of fuel cells.
	 */
	/**
	 * money - the instance of money.
	 */
	private final Item money, fuelCells, food, hullParts, fCapacitors, engines; 

	/**
	 * timeBulletBills - the instance of time bullet bills.
	 */
	/**
	 * timeSuits - the instance of time suits.
	 */
	private final Item timeSuits, timeBulletBills;

	/**
	 * sets the temporary item values to zero, creates instances of all inventory items.
	 * @param cont the Control instance that holds the game data
	 */
	public Store(Control cont){
		playerInv = cont.getInv();
		tempMoney = 0;
		tempFuelCells = 0;
		tempFood = 0;
		tempHullParts = 0;
		tempFCapacitors = 0;
		tempEngines = 0;
		
		money = new Money();
		fuelCells = new FuelCell();
		food = new Food();
		hullParts = new HullPart();
		fCapacitors = new FluxCapacitor();
		engines = new Engine();
		timeSuits = new TimeSuit();
		timeBulletBills = new TimeBulletBill();
	}

	/**
	 * adds an item to the inventory while factoring in cost
	 * PRECONDITION:THE MONEY IS NOT AN ISSUE
	 * @param amount the amount of the item to buy
	 * @param item the item to buy
	 */
	public void buyItem(int amount, Item item){
		final boolean itemCheck = playerInv.canAddItem(amount, item);
		final boolean moneyCheck = playerInv.getMoney().canRemoveCurrentAmount(
		        amount * item.getValue());
		
		if(itemCheck && moneyCheck){
			playerInv.addItem(amount, item);
			playerInv.getMoney().removeCurrentAmount(amount * item.getValue());
		}

	}
	
	/**
	 * Checks if buying an item is viable DOES NOT BUY AN ITEM
	 * @param amount the amount of the item you want to buy
	 * @param item the item you want to buy
	 * @return true if the item was bought
	 */
	public boolean canBuyItem(int amount, Item item){
		final boolean itemCheck = playerInv.canAddItem(amount, item);
		final boolean moneyCheck = playerInv.getMoney().canRemoveCurrentAmount(
		        amount * item.getValue());
			
		return itemCheck && moneyCheck;
	}

	/**
	 * Possibility to add soon, currently not in use
	 * PRECONDITION MONEY IS NOT AN ISSUE
	 * removes an item from the inventory while also giving the player some money
	 * @param amount amount of items to be removed from the inventory 
	 * @param item the type of item to remove

	 * @return removeItem's return unless the other inventory has insufficient money */
	public boolean canSellItem(int amount, Item item){
		return false;
	}
	
	/**
	 * commits the players inventory to what was bought in the store
	 * @param cont the instance of Control that holds the game information
	 */
	public void commitInventoryChanges(Control cont){
		cont.setInv(playerInv);
	}
	
	/**
	 * getter for tempMoney
	
	 * @return tempMoney */
	public int getTempMoney() {
		return tempMoney;
	}

	/**
	 * setter for tempMoney
	 * @param tempMoney
	 */
	public void setTempMoney(int tempMoney) {
		this.tempMoney = tempMoney;
	}

	/**
	 * getter for tempFuelCells
	
	 * @return tempFuelCells */
	public int getTempFuelCells() {
		return tempFuelCells;
	}

	/**
	 * setter for tempFuelCells
	 * @param tempFuelCells
	 */
	public void setTempFuelCells(int tempFuelCells) {
		this.tempFuelCells = tempFuelCells;
	}

	/**
	 * getter for TempFood
	
	 * @return tempFood */
	public int getTempFood() {
		return tempFood;
	}

	/**
	 * setter for tempFood
	 * @param tempFood
	 */
	public void setTempFood(int tempFood) {
		this.tempFood = tempFood;
	}

	/**
	 * getter for TempHullParts
	
	 * @return tempHullParts */
	public int getTempHullParts() {
		return tempHullParts;
	}
	
	/**
	 * setter for TempHullParts
	 * @param tempHullParts
	 */
	public void setTempHullParts(int tempHullParts) {
		this.tempHullParts = tempHullParts;
	}
	
	/**
	 * getter for tempFCapacitors
	
	 * @return tempFCapacitors */
	public int getTempFCapacitors() {
		return tempFCapacitors;
	}
	
	/**
	 * setter for tempFCapacitors
	 * @param tempFCapacitors 
	 */
	public void setTempFCapacitors(int tempFCapacitors) {
		this.tempFCapacitors = tempFCapacitors;
	}
	
	/**
	 * getter for tempEngines
	
	 * @return tempEngines */
	public int getTempEngines() {
		return tempEngines;
	}

	/**
	 * setter for tempEngines
	 * @param tempEngines
	 */
	public void setTempEngines(int tempEngines) {
		this.tempEngines = tempEngines;
	}
	
	/**
	 * setter for tempTimeBulletBills.
	 * @param tempTimeBulletBills
	 */
	public void setTempTimeBulletBills(int tempTimeBulletBills){
		this.tempTimeBulletBills = tempTimeBulletBills;
	}
	
	/**
	 * setter for TempTimeSuits.
	 * @param tempTimeSuits
	 */
	public void setTempTimeSuits(int tempTimeSuits){
		this.tempTimesuits = tempTimeSuits;
	}

	/**
	 * sums up the current temporary weight added to the actual weight
	
	 * @return the sum of all the temp weights added to the current weight */
	public int getTempWeight() {
		return playerInv.getInvWeight()
		        + tempMoney * money.getWeight()
		        + tempFuelCells * fuelCells.getWeight()
		        + tempFood * food.getWeight() 
				+ tempHullParts * hullParts.getWeight()
				+ tempFCapacitors * fCapacitors.getWeight()
				+ tempEngines * engines.getWeight() 
				+ tempTimesuits * timeSuits.getWeight() 
				+ tempTimeBulletBills * timeBulletBills.getWeight();
	}
	
	/**
	 * Sums the balance temporarily for gui purposes
	
	 * @return the summation of the temporary variables minus the current money amount */
	public int getTempCost() {
		return playerInv.getMoney().getCurrentAmount()
		        - ((tempMoney * money.getValue()) 
		        + (tempFuelCells * fuelCells.getValue())
				+ (tempFood * food.getValue()) 
				+ (tempHullParts * hullParts.getValue())
				+ (tempFCapacitors * fCapacitors.getValue()) 
				+ (tempEngines * engines.getValue()) 
				+ tempTimesuits * timeSuits.getValue() 
				+ tempTimeBulletBills * timeBulletBills.getValue());
	}
	
	/**
	 * returns the string representation of the store
	 * @return the string representation of the store
	 */
	public String toString(){
	    return "Store"; //TODO come up with an actual string representation
	}
	
}
